package com.top.art.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.top.art.bo.RulerBO;
import com.top.art.bo.RulerQueryBO;
import com.top.art.bo.TextValueBO;
import com.top.art.entity.Ruler;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.List;

/**
 * The interface Ruler converter.
 *
 * @author hjh
 * @version 2020 /10/7-17:41
 */
@Mapper(componentModel = "spring", imports = {JSON.class, JSONObject.class, TypeReference.class, TextValueBO.class, List.class})
public interface RulerConverter {

    /**
     * Bo 2 do ruler.
     *
     * @param bo the bo
     * @return the ruler
     */
    @Mappings({
            @Mapping(target = "scoreRuler", expression = "java(JSON.toJSONString(bo.getScoreRulers()))"),
            @Mapping(target = "example", expression = "java(JSON.toJSONString(bo.getExamples()))")
    })
    Ruler bo2do(RulerBO bo);

    /**
     * Bo 2 do ruler bo.
     *
     * @param model the model
     * @return the ruler bo
     */
    @Mappings({
            @Mapping(target = "scoreRulers", expression = "java(JSONObject.parseObject(model.getScoreRuler(), new TypeReference<List<TextValueBO>>() {}))"),
            @Mapping(target = "examples", expression = "java(JSONObject.parseObject(model.getExample(), new TypeReference<List<TextValueBO>>() {}))")
    })
    RulerBO do2bo(Ruler model);

    /**
     * Bo 2 do ruler.
     *
     * @param bo the bo
     * @return the ruler
     */
    @Mappings({
            @Mapping(target = "scoreRuler", expression = "java(JSON.toJSONString(bo.getScoreRulers()))"),
            @Mapping(target = "example", expression = "java(JSON.toJSONString(bo.getExamples()))")
    })
    Ruler bo2do(RulerQueryBO bo);
}
